var idsByRedio = '';

/**
 * 应付台账明细
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http){
    console.log('------$scope.menuId1-----', $scope.menuId);
    console.log('------$scope.buttonArr1-----', $scope.buttonArr);

    $scope.bindBtn = function(event) {
        var datas = $scope.getSelectedRows();
        switch(event){
            case 'event-to-add':
            {
                window.top.add_tab("应付台账明细 - 新增","/web/account/tcwNeedPayLedgerDetail/toAdd");
                break;
            }
            case 'event-to-edit'://编辑
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else if(datas.length > 1) {
                    layer.alert('只能选中一行数据');
                } else {
                    //window.top.add_tab("应付台账明细 - 编辑",'/web/account/tcwNeedPayLedgerDetail/toEdit?id=' + datas[0].id);
                    $scope.showModalLink('/web/account/tcwNeedPayLedgerDetail/toEdit?id=' + datas[0].id, '编辑');
                }
                break;
            }
            case 'LAYTABLE_EXPORT_DATA'://导出
            {
                $scope.doExport('/api/inter/tcwNeedPayLedgerDetail/export');
                break;
            }
            case 'event-to-doTransferFlow'://划拨
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var ids = '';
                    for(var i=0;i<datas.length;i++){
                        if(ids==''){
                            ids = datas[i].id;
                        }else {
                            ids = ids + ',' +datas[i].id;
                        }
                    }
                    layer.msg('处理中', {
                        icon: 16,shade: 0.01
                    });
                    $http.post('/api/inter/tcwNeedPayLedgerDetail/doTransferFlow', {}, {params: {ids: ids}})
                        .success(function (res) {
                            layer.closeAll();
                            layer.alert(res.msg);
                            setTimeout(function(){
                                //layer.closeAll();
                                location.reload();
                            }, 3000);
                        })
                        .error(function(result){
                            layer.close();
                            layer.msg("操作失败，请稍后再试！", {icon: 2});
                        });
                }
                break;
            }


            case 'event-to-copy'://复制
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var ids = '';
                    for(var i=0;i<datas.length;i++){
                        if(ids==''){
                            ids = datas[i].id;
                        }else {
                            ids = ids + ',' +datas[i].id;
                        }
                    }
                    layer.msg('处理中', { icon: 16,shade: 0.01 });
                    $http.post('/api/inter/tcwNeedPayLedgerDetail/doCopy', {}, {params: {ids: ids}})
                        .success(function (res) {
                            layer.closeAll();
                            layer.alert(res.msg);
                            setTimeout(function(){
                                location.reload();
                            }, 3000);
                        })
                        .error(function(result){
                            layer.close();
                            layer.msg("操作失败，请稍后再试！", {icon: 2});
                        });

                }
                break;
            }


            case 'event-to-del'://删除
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var ids = '';
                    for(var i=0;i<datas.length;i++){
                        if(ids==''){
                            ids = datas[i].id;
                        }else {
                            ids = ids + ',' +datas[i].id;
                        }
                    }
                    layer.msg('处理中', { icon: 16,shade: 0.01 });
                    $http.post('/api/inter/tcwNeedPayLedgerDetail/doDelByList', {}, {params: {ids: ids}})
                        .success(function (res) {
                            layer.closeAll();
                            layer.alert(res.msg);
                            setTimeout(function(){
                                location.reload();
                            }, 3000);
                        })
                        .error(function(result){
                            layer.close();
                            layer.msg("操作失败，请稍后再试！", {icon: 2});
                        });

                }
                break;
            }


            case 'event-to-setUnitPrice'://批量设置成本价
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var ids = '';
                    for(var i=0;i<datas.length;i++){
                        if(ids==''){
                            ids = datas[i].id;
                        }else {
                            ids = ids + ',' +datas[i].id;
                        }
                    }
                    var util = layui.util;
                    layer.prompt({title: '请输入成本价'}, function(value, index, elem){
                        if(value === '') return elem.focus();
                        var unitPrice = util.escape(value);
                        // 关闭 prompt
                        layer.close(index);

                        layer.msg('处理中', { icon: 16,shade: 0.01 });
                        $http.post('/api/inter/tcwNeedPayLedgerDetail/setUnitPrice', {}, {params: {ids: ids,unitPrice: unitPrice}})
                            .success(function (res) {
                                layer.closeAll();
                                layer.alert(res.msg);
                                setTimeout(function(){
                                    location.reload();
                                }, 3000);
                            })
                            .error(function(result){
                                layer.close();
                                layer.msg("操作失败，请稍后再试！", {icon: 2});
                            });

                    });


                }
                break;
            }



            case 'event-to-setProxyPrice'://批量设置代理价
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {
                    var ids = '';
                    for(var i=0;i<datas.length;i++){
                        if(ids==''){
                            ids = datas[i].id;
                        }else {
                            ids = ids + ',' +datas[i].id;
                        }
                    }
                    var util = layui.util;
                    layer.prompt({title: '请输入代理价'}, function(value, index, elem){
                        if(value === '') return elem.focus();
                        var proxyPrice = util.escape(value);
                        // 关闭 prompt
                        layer.close(index);
                        layer.msg('处理中', { icon: 16,shade: 0.01 });

                        $http.post('/api/inter/tcwNeedPayLedgerDetail/setProxyPrice', {}, {params: {ids: ids,proxyPrice: proxyPrice}})
                            .success(function (res) {
                                layer.closeAll();
                                layer.alert(res.msg);
                                setTimeout(function(){
                                    location.reload();
                                }, 3000);
                            })
                            .error(function(result){
                                layer.close();
                                layer.msg("操作失败，请稍后再试！", {icon: 2});
                            });

                    });


                }
                break;
            }


            case 'event-to-setNeedPayType'://批量修改应付类型
            {
                if(datas.length == 0) {
                    layer.alert('尚未选中数据');
                } else {

                    for(var i=0;i<datas.length;i++){
                        if(idsByRedio==''){
                            idsByRedio = datas[i].id;
                        }else {
                            idsByRedio = idsByRedio + ',' +datas[i].id;
                        }
                    }
                    $scope.showModalRadio("TB_NeedPayType", 3, 'aaaa', null, {});
                    break;
                }
                break;
            }



        };
    }
}



/*
 * 自定义通用控制器，若无定义，页面会报错
 */
app.controller('mainSelfCtrl', ['$scope', '$http', function GridSelfController($scope, $http){

    $scope.doSelectRadioBySelf = function(data) {
        console.log('单选框特殊回调', data);
        var chooseId = data.id;
        layui.layer.msg('处理中', {
            icon: 16,
            shade: 0.01
        });
        // 新增操作
        $http.post('/api/inter/tcwNeedPayLedgerDetail/setNeedPayType', {}, {params: {ids: idsByRedio,needPayTypeCode: data.code}})
            .success(function (res) {
                layer.closeAll();
                layer.alert(res.msg);
                setTimeout(function(){
                    location.reload();
                }, 3000);
            });

    }
}]);

